const { get } = require('../../../utils/request.js')
const { timeRates,
  fixPenaltyRate,
  lendRate,
  lendDuteRate
} = require('../../../utils/config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    settingValue: "",
    settingName: "",
    settingVale: 1,
    list: [{
      id: "timeYearRate",
      name: "活期年化利率",
      rateArray: timeRates,
      value: "0",
      split: false
    }, {
      id: "fixRate",
      name: "定期年化利率",
      value: "不同的利率",
      route: "/pages/me/rate/fixRateSet/fixRateSet",
      split: false
    }, {
      id: "fixTimePenalty",
      name: "定期存款违约金比例",
      rateArray: fixPenaltyRate,
      value: "0",
      split: false
    }, {
      id: "lendRate",
      name: "借出年化利率",
      rateArray: lendRate,
      value: "0",
      split: false
    }, {
      id: "lendPenalty",
      name: "借出逾期罚息利率",
      rateArray: lendDuteRate,
      value: "0",
      split: false
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const unicode = wx.getStorageSync('unicode')
    get('familyconfig/list',
      {
        'filter_familyUnicode_eq_s': unicode,
        'filter_name_in_ss': 'timeYearRate,fixTimePenalty,lendRate,lendPenalty'
      },
      (data) => {
        data.forEach(item => {
          this.data.list.forEach(o => {
            if (o.id == item.name) {
              o.value = item.value
              o.dataId = item.id
            }
          })
        })

        this.setData({
          list: this.data.list
        })
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  exit() {
    this.setData({ show: false })
  },

  onTouchItem({ currentTarget: { id } }) {
    const item = this.data.list[id];
    let index = item.rateArray.indexOf(item.value + '%');
    item.active = true;

    this.setData({
      list: this.data.list,
      settingValue: index
    })
  },
  onTouchEndItem({ currentTarget: { id } }) {
    const list = this.data.list;
    list[id].active = false;
    this.setData({
      list
    });

    if (list[id].route) {
      wx.navigateTo({
        url: list[id].route,
      })
    }

  },
  bindRateChange({
    currentTarget: { dataset: { rateIndex } },
    detail: { value }
  }) {
    const item = this.data.list[rateIndex];
    item.value = item.rateArray[value].replace('%', '');

    this.setData({
      list: this.data.list
    })

    get('familyconfig/updatefield', {
      "id": this.data.list[rateIndex].dataId,
      "key": "value",
      "value": this.data.list[rateIndex].value
    })

  }
})